package com.ejlchina.tool.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.LayoutInputSwitchBinding

class SwitchViewGroup(context: Context, attributeSet: AttributeSet): LinearLayout(context,attributeSet) {
    val binding: LayoutInputSwitchBinding

    init {
        binding = LayoutInputSwitchBinding.inflate(LayoutInflater.from(context),this,true)
    }

    private fun onState(){
        binding.tvOn.setTextColor(context.getColor(R.color.white))
        binding.tvOff.setTextColor(context.getColor(R.color.color_2c))
        binding.tvOn.setBackgroundResource(R.drawable.shape_co_8_so_1f39ff)
        binding.tvOff.setBackgroundResource(R.drawable.shape_co_8_so_f7f7f7)
    }

    private fun offState(){
        binding.tvOn.setTextColor(context.getColor(R.color.color_2c))
        binding.tvOff.setTextColor(context.getColor(R.color.white))
        binding.tvOn.setBackgroundResource(R.drawable.shape_co_8_so_f7f7f7)
        binding.tvOff.setBackgroundResource(R.drawable.shape_co_8_so_1f39ff)
    }

    fun setTurnOnAndOff(onBlock: () -> Unit, offBlock: () -> Unit){
        binding.tvOn.setOnClickListener {
            onState()
            onBlock()
        }
        binding.tvOff.setOnClickListener {
            offState()
            offBlock()
        }
    }

    fun setState(isOn: Boolean){
        if (isOn) onState() else offState()
    }
}